package com.sram.action;

import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.opensymphony.xwork2.ActionSupport;
import com.sram.entity.User;
import com.sram.service.UserService;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserAction extends ActionSupport  implements SessionAware {
	private User user;
	private List<User> users;
	private User u;
	private String id;
	private UserService userService;
	private HttpServletRequest request = ServletActionContext.getRequest();
	private File uploadify;//用于上传

	public File getHeadImg() {
		return uploadify;
	}

	public void setHeadImg(File uploadify) {
		this.uploadify = uploadify;
	}


	public User getU() {
		return u;
	}

	public void setU(User u) {
		this.u = u;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String regist(){
		userService.saveUser(u);
		return SUCCESS;
	}
	//第一种传参数
	public String login(){
//		User online_user = userService.login(u.getUserName(), u.getPassword());
//		if(online_user == null){
//			return ERROR;
//		}
//		System.out.printf(u.getUserName()+u.getPassword());
//		System.out.printf(u.toString());
//		System.out.println("上传照片：");
//
//		System.out.println("----------------------------------");
//		System.out.println(headImg.getAbsolutePath());
//		System.out.println(headImgContentType);
//		System.out.println(headImgFileName);
//		String realpath = ServletActionContext.getServletContext().getRealPath("/images");
//		File file = new File(realpath);
//		if(!file.exists()) file.mkdirs();
//		try {
//			FileUtils.copyFile(headImg, new File(file, headImgFileName));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		return SUCCESS;
	}
	//第2种传参数
	public String login2(){
		String name = request.getParameter("name");
		System.out.print(id);
		System.out.print(name);
		return SUCCESS;
	}

	//AJAX
	public void getOne() throws IOException {
		user= new User();
		user.setId(121);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext()
				.get(ServletActionContext.HTTP_RESPONSE);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(user.toString());

	}
	//AJAX2
	public String getAll() {
		users= new ArrayList<User>();
		for(int i=0;i<10;i++){
			User user= new User();

			user.setId(i);
			users.add(user);

		}

		return "asd";
	}
	public void uploads() throws IOException {
//		try {
//			Thread.sleep(5000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		System.out.println("执行了execute方法：");
		System.out.println(u);
 		File file = new File("D:/duhao");
		if(!file.exists()) file.mkdirs();
		try {
			FileUtils.copyFile(uploadify, new File(file, "duha.zip"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext()
				.get(ServletActionContext.HTTP_RESPONSE);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("0000");
//		return SUCCESS;
	}

	public void setSession(Map<String, Object> session) {

	}
}
